
package com.ostrich.note.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.note.event.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;


public abstract class BaseFragment<VDB extends ViewDataBinding> extends Fragment {
    protected VDB binding;
    public BaseFragment() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,getLayout(),container,false);
        initView();
        initListener();
        initData();
        //注册广播
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    //接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receive(Event event){
        //判断是否为自己发送的广播
        if (event.target.equals(getClass().getSimpleName())){
            onEvent(event);
        }
    }
    //子类重写该方法就可以处理接受数据，不要在base里面做具体的处理
    public void onEvent(Event event){}

    public abstract int getLayout();
    public abstract void initData();
    public abstract void initView();
    public abstract void initListener();

    public void startActivity(Class tClass){
        Intent intent = new Intent(getActivity(), tClass);
        startActivity(intent);
    }

    public void startActivityBundle(Class tClass,Bundle bundle){
        Intent intent = new Intent(getActivity(), tClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}